package app.transmission.remote.common;

import android.app.Application;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class TranSettings extends Application {
	
	private String host;
	
	private String port;
	
	private String user;
	
	private String password;

	private boolean remember;
	
	private String url;
	
	@Override
	public void onCreate() {
		super.onCreate();
		SharedPreferences preferences = this.getSharedPreferences("settings", MODE_PRIVATE);
		host = preferences.getString("host", "");
		port = preferences.getString("port", "9091");
		user = preferences.getString("user", "");
		password = preferences.getString("password", "");
		remember = preferences.getBoolean("remember", false);
	}
	
	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public String getPort() {
		return port;
	}

	public void setPort(String port) {
		this.port = port;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUrl() {
		if (url == null)
			updateUrl();
		return url;
	}

	public boolean isRemember() {
		return remember;
	}

	public void setRemember(boolean remember) {
		this.remember = remember;
	}
	
	private void updateUrl() {
		url = new StringBuilder().append("http://").append(host)
				.append(":").append(port).append("/transmission/rpc").toString();
	}
	
	public void saveSettings() {
		Editor editor = this.getSharedPreferences("settings", MODE_PRIVATE).edit();
		editor.putString("host", host);
		editor.putString("port", port);
		editor.putString("user", user);
		editor.putString("password", password);
		editor.putBoolean("remember", true);
		editor.commit();
	}
	
}
